package org.nidy.erp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nidy.erp.mapper.IGoodsMapper;
import org.nidy.erp.pojo.Goods;
import org.nidy.erp.pojo.ItemClass;
import org.nidy.erp.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("goodsService")
public class GoodsService extends ServiceImpl<IGoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private IGoodsMapper iGoodsMapper;

    @Override
    public List<ItemClass> getBigHomeItemClass() {
        List<ItemClass> allClasses = iGoodsMapper.getItemClass();
        return buildTree(allClasses,230);
    }

    @Override
    public List<Goods> getGoodsList() {
        List<Goods> goodsList = iGoodsMapper.getGoodsList();
        return goodsList;
    }

    private List<ItemClass> buildTree(List<ItemClass> nodes,Integer parentId) {
        return nodes.stream()
                .filter(node -> {
                    // 处理 null 的情况，使用 Objects.equals 更安全
                    return Objects.equals(node.getPID(), parentId);
                })
                .peek(node -> node.setChildren(buildTree(nodes, node.getId())))
                .collect(Collectors.toList());
    }
}
